﻿using System;

public class Car
{
    // 这个委托来与Car事件协作
    public delegate void CarEngineHandler(string msg);

    // 这种汽车可以发送这些事件
    public event CarEngineHandler Exploded;
    public event CarEngineHandler AboutToBlow;

    public int CurrentSpeed { get; set; }
    public int MaxSpeed { get; set; }
    public string PetName { get; set; }

    private bool CarIsDead;

    public Car()
    {
        MaxSpeed = 100;
    }

    public Car(string name, int maxSp, int currSp)
    {
        PetName = name;
        MaxSpeed = maxSp;
        CurrentSpeed = currSp;
    }

    public void Accelerate(int delta)
    {
        // 如果Car无法使用，触发Exploded事件
        if (CarIsDead)
        {
            if (Exploded != null)
            {
                Exploded("sorry, this car is dead");
            }
        }
        else
        {
            CurrentSpeed += delta;

            // 确认已无法使用，触发AboutToBlow事件
            if ((MaxSpeed - CurrentSpeed) == 10 && AboutToBlow != null)
            {
                AboutToBlow("careful buddy ! gonna blow !");
            }

            if(CurrentSpeed >= MaxSpeed)
            {
                CarIsDead = true;
            }
            else
            {
                Console.WriteLine(@"$CurrentSpeed={CurrentSpeed}");
            }
        }
    }
}
/* 写一个最简单的使用事件的代码。帮助理解。 */